package leecode.LinkedList.DoublePoint;


public class RemoveNthFromEnd {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }


    public static ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0,head);
        ListNode slow=dummy;
        ListNode fast=head;

        for (int i = 1; i < n; i++) {
            fast=fast.next;
        }

        while (fast!=null && fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }

        slow.next=slow.next.next;
        return head;
    }
}
